
package cn.appliedata.base.eureka.listener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class EurekaStateChangeListener {
    private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");


    /**
     * 服务下线事件
     *
     * @param eurekaInstanceCanceledEvent
     */

    @EventListener
    public void listen(EurekaInstanceCanceledEvent eurekaInstanceCanceledEvent) {
        //TODO 邮件通知或短信通知
        log.warn("[服务下线] {}，time：{}", eurekaInstanceCanceledEvent.toString(), sdf.format(eurekaInstanceCanceledEvent.getTimestamp()));
    }


    /**
     * 服务注册事件
     *
     * @param eurekaInstanceRegisteredEvent
     */

    @EventListener
    public void listen(EurekaInstanceRegisteredEvent eurekaInstanceRegisteredEvent) {
        //TODO 邮件通知或短信通知
        log.warn("[服务启动] {}", eurekaInstanceRegisteredEvent);
    }

    /**
     * 服务续约事件
     *
     * @param event
     */

    @EventListener
    public void listen(EurekaInstanceRenewedEvent eurekaInstanceRenewedEvent) {
        //TODO 邮件通知或短信通知
        log.warn("[服务续约] {}", eurekaInstanceRenewedEvent);
    }

    /**
     * Eureka注册中心启动事件
     *
     * @param eurekaRegistryAvailableEvent
     */

    @EventListener
    public void listen(EurekaRegistryAvailableEvent eurekaRegistryAvailableEvent) {
        log.info("[Eureka注册中心启动] {}", eurekaRegistryAvailableEvent);
    }

    /**
     * Eureka Server启动事件
     *
     * @param event
     */

    @EventListener
    public void listen(EurekaServerStartedEvent eurekaServerStartedEvent) {
        log.info("[EurekaServer启动] {}", eurekaServerStartedEvent);
    }
}
